package org.csu.hospital.controller;

import com.alibaba.fastjson.JSONObject;
import org.csu.hospital.domain.Medicine;
import com.github.pagehelper.PageInfo;
import org.csu.hospital.service.medicineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/backstage/medicine/")
public class MedicineController {

    @Autowired
    private medicineService medicineService;

    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> list(int page, int limit) {
        int before = limit * (page - 1) + 1;
        int after = page * limit;
        List<Medicine> medicines = medicineService.listAllMedicine(before, after);
        int count = medicineService.countMedicine();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("msg", "");
        map.put("code", 0);
        map.put("count", count);
        map.put("data", medicines);
        System.out.println(map);
        return map;
    }

    @RequestMapping("toEdit")
    public String toDeptEdit(int medicineId, String name, String category, int number, int price, Model model) throws UnsupportedEncodingException {
        Medicine medicine=new Medicine();
        medicine.setMedicineId(medicineId);
        medicine.setName(name);
        medicine.setCategory(category);
        medicine.setNumber(number);
        medicine.setPrice(price);
        medicine.setPharmacyId(1000);
        model.addAttribute("medicine",medicine);
        return "backstage/page/medicine/medicine_edit";
    }

    @RequestMapping("toUpdate")
    @ResponseBody
    public String toUpdate(@RequestParam("oid") String oid, @RequestParam("medicineId") String medicineId, @RequestParam("category") String category
            , @RequestParam("name") String name, @RequestParam("number") String number, @RequestParam("price") String price) {
        System.out.println(oid + medicineId + category + name + number + price);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", "1");

        Medicine medicine = new Medicine();
        medicine.setMedicineId(Integer.parseInt(medicineId));
        medicine.setPharmacyId(1000);
        medicine.setCategory(category);
        medicine.setName(name);
        medicine.setNumber(Integer.parseInt(number));
        medicine.setPrice(Integer.parseInt(price));

        try {
            medicineService.updateMedicine(medicine,Integer.parseInt(oid));
        } catch (Exception e) {
            jsonObject.put("result", "0");
            e.printStackTrace();
        }
        System.out.println(jsonObject);
        return jsonObject.toString();
    }

    @RequestMapping("toAdd")
    public String toAdd(Model model)
    {
        int count = medicineService.countMedicine();
        count++;
        model.addAttribute("count",count);
        return "backstage/page/medicine/medicine_add";
    }

    @RequestMapping("toInsert")
    @ResponseBody
    public String toInsert(@RequestParam("medicineId") String medicineId, @RequestParam("category") String category
            , @RequestParam("name") String name, @RequestParam("number") String number, @RequestParam("price") String price) {
        System.out.println( medicineId + category + name + number + price);
        String result="1";

        Medicine medicine = new Medicine();
        medicine.setMedicineId(Integer.parseInt(medicineId));
        medicine.setPharmacyId(1000);
        medicine.setCategory(category);
        medicine.setName(name);
        medicine.setNumber(Integer.parseInt(number));
        medicine.setPrice(Integer.parseInt(price));

        try {
            medicineService.insertMedicine(medicine);
        } catch (Exception e) {
            result="0";
            e.printStackTrace();
        }
        System.out.println(result);
        return result;
    }
}
